-- Tags: use-rocksdb, long, no-object-storage

SET join_algorithm = 'direct';

{% for table_size in [10, 65555, 100000] -%}
DROP TABLE IF EXISTS rdb_{{ table_size }};
{% endfor -%}

{% for table_size in [10, 65555, 100000] -%}

CREATE TABLE rdb_{{ table_size }} (key UInt64, value String) ENGINE = EmbeddedRocksDB PRIMARY KEY (key);
INSERT INTO rdb_{{ table_size }}
    SELECT (sipHash64(number) % {{ table_size }}) as key, ('val' || toString(key)) AS value
    FROM numbers_mt({{ table_size }});

{% for right_size in [table_size // 2, table_size + table_size // 4 + 1] -%}

SELECT count() == (SELECT count() FROM rdb_{{ table_size }} WHERE key < {{ right_size }})
FROM (SELECT number as k FROM numbers_mt({{ right_size }})) as t1
INNER JOIN rdb_{{ table_size }} as rdb
ON rdb.key == t1.k
{% if table_size < 100 %}SETTINGS max_block_size = 1{% endif -%}
;

SELECT count() == {{ right_size }} and countIf(value != '') == (SELECT count() FROM rdb_{{ table_size }} WHERE key < {{ right_size }})
FROM (SELECT number as k FROM numbers_mt({{ right_size }})) as t1
LEFT JOIN rdb_{{ table_size }} as rdb
ON rdb.key == t1.k
{% if table_size < 100 %}SETTINGS max_block_size = 1{% endif -%}
;

{% endfor -%}
{% endfor -%}

{% for table_size in [10, 65555, 100000] -%}
DROP TABLE IF EXISTS rdb_{{ table_size }};
{% endfor -%}
